%% @author Administrator
%% @doc 发送消息给客户端，与游戏端消息接收和发送


-module(net_router_svr).
-behaviour(gen_server).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

%% ====================================================================
%% API functions
%% ====================================================================
-export([start_link/2]).

-include("proto.hrl").

%% ====================================================================
%% Behavioural functions
%% ====================================================================
-record(state, {sock = undefined,
				uid = 0,
				user_svr_pid = undefined}).

%% init/1
%% ====================================================================
%% @doc <a href="http://www.erlang.org/doc/man/gen_server.html#Module:init-1">gen_server:init/1</a>
-spec init(Args :: term()) -> Result when
	Result :: {ok, State}
			| {ok, State, Timeout}
			| {ok, State, hibernate}
			| {stop, Reason :: term()}
			| ignore,
	State :: term(),
	Timeout :: non_neg_integer() | infinity.
%% ====================================================================
init([Sock, UserSvrPid]) ->
    {ok, #state{sock = Sock, user_svr_pid = UserSvrPid}}.


%% handle_call/3
%% ====================================================================
%% @doc <a href="http://www.erlang.org/doc/man/gen_server.html#Module:handle_call-3">gen_server:handle_call/3</a>
-spec handle_call(Request :: term(), From :: {pid(), Tag :: term()}, State :: term()) -> Result when
	Result :: {reply, Reply, NewState}
			| {reply, Reply, NewState, Timeout}
			| {reply, Reply, NewState, hibernate}
			| {noreply, NewState}
			| {noreply, NewState, Timeout}
			| {noreply, NewState, hibernate}
			| {stop, Reason, Reply, NewState}
			| {stop, Reason, NewState},
	Reply :: term(),
	NewState :: term(),
	Timeout :: non_neg_integer() | infinity,
	Reason :: term().
%% ====================================================================
handle_call(Request, From, State) ->
    Reply = ok,
    {reply, Reply, State}.


%% handle_cast/2
%% ====================================================================
%% @doc <a href="http://www.erlang.org/doc/man/gen_server.html#Module:handle_cast-2">gen_server:handle_cast/2</a>
-spec handle_cast(Request :: term(), State :: term()) -> Result when
	Result :: {noreply, NewState}
			| {noreply, NewState, Timeout}
			| {noreply, NewState, hibernate}
			| {stop, Reason :: term(), NewState},
	NewState :: term(),
	Timeout :: non_neg_integer() | infinity.
%% ====================================================================
handle_cast(Msg, State) ->
    {noreply, State}.


%% handle_info/2
%% ====================================================================
%% @doc <a href="http://www.erlang.org/doc/man/gen_server.html#Module:handle_info-2">gen_server:handle_info/2</a>
-spec handle_info(Info :: timeout | term(), State :: term()) -> Result when
	Result :: {noreply, NewState}
			| {noreply, NewState, Timeout}
			| {noreply, NewState, hibernate}
			| {stop, Reason :: term(), NewState},
	NewState :: term(),
	Timeout :: non_neg_integer() | infinity.
%% ====================================================================
handle_info({recv_client_msg, BinMsg}, #state{uid = Uid, user_svr_pid = UserSvrPid} = State) ->
	MsgId = ?PROTO_MSG_LOGIN,
	Msg = util_msg:decode_msg(MsgId, BinMsg),
	io:format("Receive Msg:~p From [~p]  Data:~p~n",[MsgId, Uid, Msg]),
	%% 将数据分配给用户
	UserSvrPid ! {recv_client_msg, MsgId, Msg},
    {noreply, State};

handle_info({send_client_msg, MsgId, Msg}, #state{uid = Uid, sock = Socket, user_svr_pid = UserSvrPid} = State) ->
	BinMsg = util_msg:encode_msg(MsgId, Msg),
	case gen_tcp:send(Socket, BinMsg) of
		ok ->
			io:format("Send Msg:~p From [~p]  Data:~p~n",[MsgId, Uid, Msg]);
		{error, Reason} ->
			io:format("SendMsg Fail Reason:~p~n",[Reason]),
			io:format("FailData: Uid:~p MsgId:~p Msg:~p~n",[Uid, MsgId, Msg])
	end,
    {noreply, State};

handle_info(Info, State) ->
    {noreply, State}.


%% terminate/2
%% ====================================================================
%% @doc <a href="http://www.erlang.org/doc/man/gen_server.html#Module:terminate-2">gen_server:terminate/2</a>
-spec terminate(Reason, State :: term()) -> Any :: term() when
	Reason :: normal
			| shutdown
			| {shutdown, term()}
			| term().
%% ====================================================================
terminate(Reason, State) ->
    ok.


%% code_change/3
%% ====================================================================
%% @doc <a href="http://www.erlang.org/doc/man/gen_server.html#Module:code_change-3">gen_server:code_change/3</a>
-spec code_change(OldVsn, State :: term(), Extra :: term()) -> Result when
	Result :: {ok, NewState :: term()} | {error, Reason :: term()},
	OldVsn :: Vsn | {down, Vsn},
	Vsn :: term().
%% ====================================================================
code_change(OldVsn, State, Extra) ->
    {ok, State}.


%% ====================================================================
%% Internal functions
%% ====================================================================

start_link(Sock, UserSvrPid) ->
	gen_server:start_link({local, ?MODULE}, ?MODULE, [Sock, UserSvrPid], []).


	




